package org.ronghao.androidwifiplatfrom.utils;

import java.io.File;
import java.lang.reflect.Field;

import org.ronghao.androidwifiplatfrom.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.view.Window;
import android.view.WindowManager;

public class AndroidUtil {
	/**
	 * 全屏显示
	 * 
	 * @author ronghao
	 * @data 2012-11-19 下午5:04:13
	 * @param context
	 */
	public static void setNoTitleBar_Fullscreen(Activity context) {
		context.setTheme(android.R.style.Theme_Black_NoTitleBar_Fullscreen);
		context.requestWindowFeature(Window.FEATURE_NO_TITLE);
		context.getWindow().setFlags(
				WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	/**
	 * 获得游戏照片
	 * 
	 * @author ronghao
	 * @data 2012-11-19 下午5:03:31
	 * @return
	 */
	public static File[] getPicFiles() {
		File file = new File(Environment.getExternalStorageDirectory() + "/"
				+ Contants.APP_LOCATION + "/" + Contants.APP_PICFILEPOS);
		if (file != null && file.exists()) {
			File[] files = file.listFiles();
			return files;
		} else {
			file.mkdirs();
		}
		return null;
	}

	/**
	 * 读取并缩放图片
	 * 
	 * @param content
	 * @param imagePath
	 *            图片地址
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @return Bitmap
	 */
	public static Bitmap readBitMap(Context content, String imagePath,
			int width, int height) {
		BitmapFactory.Options options = null;
		if (width > 0 && height > 0) {
			options = new BitmapFactory.Options();
			options.inJustDecodeBounds = true;
			options.inPreferredConfig = Bitmap.Config.ARGB_8888;
			options.inPurgeable = true;
			options.inInputShareable = true;

			float scaleW = options.outWidth / width;
			float scaleH = options.outHeight / height;
			if (scaleW > 1 && scaleH > 1) {
				if (scaleW > scaleH) {
					options.inSampleSize = (int) scaleW;
				} else {
					options.inSampleSize = (int) scaleH;
				}
			} else {
				options.inSampleSize = 2;
			}
			options.inJustDecodeBounds = false;
		}
		try {
			return BitmapFactory.decodeFile(imagePath, options);
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 通过反射获得资源
	 * @author ronghao
	 * @data 2012-11-20    下午7:57:46
	 * @param type
	 * @return
	 */
	public static int getPic(String type) {
		try {
			Field field = R.drawable.class.getField(type);
			int i = field.getInt(new R.drawable());
			return i;
		} catch (Exception e) {
			return R.drawable.ic_launcher;
		}
	}
}
